#include "gamewidget.h"
#include "ui_gamewidget.h"
#include <QPainter>
#include <QDir>
#include <QMessageBox>
#include <QDebug>
#include <QDesktopWidget>

GameWidget::GameWidget(QWidget *parent) :
    QDialog(parent),
    st(":/words/data.ini",QSettings::IniFormat),
    ui(new Ui::GameWidget)
{
    ui->setupUi(this);
    initWsVec();
    initButtonsVec();
    showQuestion();
}

GameWidget::~GameWidget()
{
    delete ui;
}

void GameWidget::paintEvent(QPaintEvent *event){
    QPainter painter(this);
    QPixmap pixmap=QPixmap(":/pic/background.png").scaled(size());
    painter.drawPixmap(0,0,pixmap);
}



void GameWidget::initWsVec(){
    wsList.clear();
    wsList.push_back(ui->w0);
    wsList.push_back(ui->w1);
    wsList.push_back(ui->w2);
    wsList.push_back(ui->w3);
    clearWs();
    for(auto &p:wsList){
        p->setTextMargins(0,0,0,0);
    }
}

void GameWidget::initButtonsVec(){
    buttonsList.clear();
    buttonsList.push_back(ui->pushButton0);
    buttonsList.push_back(ui->pushButton1);
    buttonsList.push_back(ui->pushButton2);
    buttonsList.push_back(ui->pushButton3);
    buttonsList.push_back(ui->pushButton4);
    buttonsList.push_back(ui->pushButton5);
    buttonsList.push_back(ui->pushButton6);
    buttonsList.push_back(ui->pushButton7);
    buttonsList.push_back(ui->pushButton8);
    buttonsList.push_back(ui->pushButton9);
    buttonsList.push_back(ui->pushButton10);
    buttonsList.push_back(ui->pushButton11);
    buttonsList.push_back(ui->pushButton12);
    buttonsList.push_back(ui->pushButton13);
    buttonsList.push_back(ui->pushButton14);
    buttonsList.push_back(ui->pushButton15);
    clearButtons();
    for(auto &p:buttonsList){
        p->installEventFilter(this);
        p->setContentsMargins(0,0,0,0);
    }
}

void GameWidget::clearWs(){
    for(auto p:wsList){
        p->clear();
    }
}

void GameWidget::clearButtons(){
    for(auto p:buttonsList){
        p->setText("");
    }
}

bool GameWidget::eventFilter(QObject *obj, QEvent *ev){
    if(buttonsList.indexOf(static_cast<QPushButton*>(obj))!=-1&&ev->type()==QEvent::MouseButtonPress){
        if(addAWords(static_cast<QPushButton*>(obj)->text())){
            QString word=getText();
            if(core.checkAnswer(word)){
                QString mean=st.value("means/"+word,"未找到释义").toString();
                QMessageBox::information(this,"恭喜","回答正确\n"+word+":"+mean);
            }else{
                QMessageBox::information(this,"提示","回答错误");
            }
            showQuestion();
            return true;
        }else{
            return QWidget::eventFilter(obj,ev);
        }
    }
    return QWidget::eventFilter(obj,ev);
}

void GameWidget::showQuestion(){
    clearWs();
    ui->showArea->setPixmap(QPixmap(":/data/"+core.getQuestion().fileName+".png").scaled(ui->showArea->size()));
    setUiWord(core.getQuestion().showUiWords);
}

bool GameWidget::addAWords(QString words){
    for(int i=0;i<4;++i){
        if(wsList[i]->text().isEmpty()){
            wsList[i]->setText(words);
            break;
        }
    }
    return !wsList[3]->text().isEmpty();
}

void GameWidget::setUiWord(QString words){
    if(words.length()!=16)
        return;
    for(int i=0;i<16;++i){
        buttonsList[i]->setText(words.mid(i,1));
    }
}

QString GameWidget::getText(){
    QString retStr;
    for(auto p:wsList){
        retStr+=p->text();
    }
    return retStr;
}

void GameWidget::on_nextQuestion_clicked()
{
    core.makeQuestion();
    showQuestion();
}

void GameWidget::on_backSpace_clicked()
{
    for(int i=3;i>=0;--i){
        if(!wsList[i]->text().isEmpty()){
            wsList[i]->clear();
            return;
        }
    }
}

void GameWidget::resizeEvent(QResizeEvent *){
    move(0,0);
    ui->showArea->setPixmap(QPixmap(":/data/"+core.getQuestion().fileName+".png").scaled(ui->showArea->size()));
}

void GameWidget::closeEvent(QCloseEvent *e){
    e->ignore();
    if(QMessageBox::question(this,"保存","是否保存当前进度？",QMessageBox::No,QMessageBox::Yes)==QMessageBox::Yes){
        core.saveSettings();
    }
    hide();
}
